export const ELEMENT_TYPE_SLIDE = 'slide';
const Slide = {
    create: (metaData, uiInterface) => {
        uiInterface = uiInterface || {
            create: () => {},
        };
        const core = {
            metaData,
            id: metaData.getIn(['element', 'id']),
            uiInterface,
            actualIndex: undefined,
            index: undefined,
            onPrev: function onPrev() {
                if (core.index > 0) {
                    core.index -= 1;
                } else {
                    core.index = core.total - 1;
                }
                core.actualIndex = core.getActualIndex(core.index);
                core.uiInterface.update(core);
            },
            onNext: function onNext() {
                if (core.index < core.total - 1) {
                    core.index += 1;
                } else {
                    core.index = 0;
                }
                core.actualIndex = core.getActualIndex(core.index);
                core.uiInterface.update(core);
            },
            onIndex: function onIndex(params = {}) {
                const { index } = params;
                if (index > -1 && index < core.initTotal) {
                    core.index = index + 1;
                    if (core.index === core.total - 1) { // 最后一张
                        core.index = 0;
                    }
                    core.actualIndex = core.getActualIndex(core.index);
                    core.uiInterface.update(core);
                }
            },
            init: function init(params = {}) {
                if (!params.total) {
                    core.index = -1;
                } else {
                    core.total = params.total;
                    core.initTotal = params.total;
                    core.isAutoPlay = params.isAutoPlay;
                    core.autoPlayInterval = params.autoPlayInterval || 5;
                    core.total += 2;// 轮播图，添加前后2张图片
                    core.index = 1;// 初始值
                }
                core.actualIndex = core.getActualIndex(core.index);
                core.uiInterface.update(core);
            },
            getActualIndex: function getActualIndex(_index) {
                if (_index === 0) {
                    return (core.initTotal - 1);
                } else if (_index === core.total - 1) {
                    return 0;
                }
                return _index - 1;
            },
        };
        return core;
    },
};

export default Slide;
